# 这个文件包含ui_module.py中需要实现的其他方法

# 进度更新方法
def update_progress(self, value, operation_type=""):
    """更新进度条"""
    # 这里可以实现进度条更新逻辑
    # 由于UI中没有明确的进度条控件，这里只输出日志
    if operation_type:
        self.append_log(f"{operation_type}进度: {value}%")

# 取消处理方法
def cancel_processing(self):
    """取消视频处理"""
    if self.ffmpeg_thread and self.ffmpeg_thread.is_running:
        self.ffmpeg_thread.stop()
        self.append_log("取消帧提取")
    
    if self.depth_thread and self.depth_thread.is_running:
        self.depth_thread.stop()
        self.append_log("取消深度图生成")
    
    if self.stereo_thread and self.stereo_thread.is_running:
        self.stereo_thread.stop()
        self.append_log("取消左右眼视图生成")
    
    self.process_start_btn.setEnabled(True)
    self.process_cancel_btn.setEnabled(False)

# 左右眼合并方法
def start_merging(self):
    """开始合并左右眼视频"""
    # 检查是否已设置FFmpeg路径
    if not self.ffmpeg_path or not os.path.exists(self.ffmpeg_path):
        QMessageBox.warning(self, "警告", "请先设置FFmpeg路径")
        return
    
    # 检查是否已选择左右眼视频
    if not self.left_eye_video or not os.path.exists(self.left_eye_video):
        QMessageBox.warning(self, "警告", "请先选择左眼视频")
        return
    
    if not self.right_eye_video or not os.path.exists(self.right_eye_video):
        QMessageBox.warning(self, "警告", "请先选择右眼视频")
        return
    
    # 获取黑色线条设置
    self.black_line = self.black_line_check.isChecked()
    self.black_line_width = self.line_width_spin.value()
    
    # 获取硬件加速选项
    self.ffmpeg_accel = self.ffmpeg_accel_combo.currentText()
    
    # 禁用按钮
    self.merge_start_btn.setEnabled(False)
    self.merge_cancel_btn.setEnabled(True)
    
    # 设置输出视频路径 - 确保使用正斜杠
    output_dir = os.path.dirname(self.left_eye_video)
    output_filename = "merged_3d.mp4"
    output_video = os.path.join(output_dir, output_filename).replace("\\", "/")
    
    # 使用FFmpeg合并视频
    self.append_log("===== 开始合并左右眼视频 =====")
    self.ffmpeg_thread = FFmpegProcessor(self.ffmpeg_path, self.ffmpeg_accel)
    self.ffmpeg_thread.left_eye_video = self.left_eye_video.replace("\\", "/")
    self.ffmpeg_thread.right_eye_video = self.right_eye_video.replace("\\", "/")
    self.ffmpeg_thread.output_video = output_video
    self.ffmpeg_thread.black_line = self.black_line
    self.ffmpeg_thread.black_line_width = self.black_line_width
    self.ffmpeg_thread.operation_type = "merge_videos"
    
    self.ffmpeg_thread.progress_updated.connect(lambda p: self.update_progress(p, "合并视频"))
    self.ffmpeg_thread.log_message.connect(self.append_log)
    self.ffmpeg_thread.process_completed.connect(self.on_merge_completed)
    
    self.ffmpeg_thread.start()

def on_merge_completed(self, success, message):
    """视频合并完成回调"""
    if success:
        self.append_log(f"视频合并成功: {message}")
        QMessageBox.information(self, "成功", "左右眼视频合并完成")
    else:
        self.append_log(f"视频合并失败: {message}")
        QMessageBox.warning(self, "失败", f"左右眼视频合并失败: {message}")
    
    self.merge_start_btn.setEnabled(True)
    self.merge_cancel_btn.setEnabled(False)

def cancel_merging(self):
    """取消视频合并"""
    if self.ffmpeg_thread and self.ffmpeg_thread.is_running:
        self.ffmpeg_thread.stop()
        self.append_log("取消视频合并")
    
    self.merge_start_btn.setEnabled(True)
    self.merge_cancel_btn.setEnabled(False)

# 从帧创建视频方法
def create_video_from_frames(self, frames_dir, output_video, eye_type):
    """从帧创建视频"""
    self.append_log(f"开始创建{eye_type}视频...")
    
    # 确保路径使用正斜杠
    frames_dir = frames_dir.replace("\\", "/")
    output_video = output_video.replace("\\", "/")
    
    # 使用FFmpeg从帧创建视频
    ffmpeg_thread = FFmpegProcessor(self.ffmpeg_path, self.ffmpeg_accel)
    ffmpeg_thread.operation_type = "extract_frames"  # 复用提取帧的逻辑，但反向操作
    
    # 构建FFmpeg命令
    command = [
        self.ffmpeg_path,
        "-framerate", str(self.fps),
        "-i", os.path.join(frames_dir, "%04d.jpg").replace("\\", "/"),
        "-c:v", "libx264",
        "-pix_fmt", "yuv420p",
        "-y",  # 覆盖已存在的文件
        output_video
    ]
    
    # 创建无窗口进程
    startupinfo = None
    if platform.system() == "Windows":
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    
    try:
        process = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            startupinfo=startupinfo
        )
        
        stdout, stderr = process.communicate()
        
        if process.returncode == 0:
            self.append_log(f"{eye_type}视频创建成功: {output_video}")
            return True
        else:
            self.append_log(f"{eye_type}视频创建失败: {stderr}")
            return False
    
    except Exception as e:
        self.append_log(f"{eye_type}视频创建出错: {str(e)}")
        return False